package com.qf.Dao.impl;

import com.qf.Dao.DoctorPointDao;
import com.qf.Utils.DBUtils;
import com.qf.entity.DoctorPoint;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class DoctorPointDaoImpl implements DoctorPointDao {
    private QueryRunner qr = new QueryRunner(DBUtils.DB.getDs());

    @Override
    public List<DoctorPoint> selectByPatientName(String patientName) throws SQLException {
        String sql = "SELECT * FROM doctor_point WHERE patient_Name = ?";
        List<DoctorPoint> query = qr.query(sql, new BeanListHandler<DoctorPoint>(DoctorPoint.class), patientName);
        return query;
    }

    @Override
    public int addPatientName(DoctorPoint doctorPoint) throws SQLException {
        String sql = "INSERT INTO doctor_point (id,patient_idcard,patient_name,doctor_name,point_date,point_place) VALUES(?,?,?,?,?,?)";
        Object[] objects = {doctorPoint.getId(), doctorPoint.getPatient_Idcard(), doctorPoint.getPatient_Name(), doctorPoint.getDoctor_Name(), doctorPoint.getPoint_Date(), doctorPoint.getPoint_Place()};
        int update = qr.update(sql, objects);
        return update;
    }

    @Override
    public int updatePatientName(DoctorPoint doctorPoint) throws SQLException {
        String sql = "UPDATE doctor_point SET id = ?,doctor_name = ?,point_date = ?,point_place=? WHERE patient_Name = ? AND patient_idcard = ?";
        Object[] objects = {doctorPoint.getId(), doctorPoint.getDoctor_Name(), doctorPoint.getPoint_Date(), doctorPoint.getPoint_Place(), doctorPoint.getPatient_Name(), doctorPoint.getPatient_Idcard()};
        return qr.update(sql, objects);
    }

    @Override
    public int deletePatientName(String patientName, String patient_Idcard) throws SQLException {
        String sql = "DELETE FROM doctor_point  WHERE patient_Name = ? AND patient_idcard = ?";
        Object[] objects = {patientName, patient_Idcard};
        return qr.update(sql, objects);
    }
}
